package top.soulblack.purchasesale.controller.stock;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.soulblack.purchasesale.common.Result;
import top.soulblack.purchasesale.model.StoreHouse;
import top.soulblack.purchasesale.service.StoreHouseService;

import static top.soulblack.purchasesale.common.CodeMsg.*;

/**
 * @Author: 韦溜溜
 * @Description:仓库管理controller
 * @Date: Create in 20:00 2019/5/19
 */
@RequestMapping("/store_management")
@Controller
public class StoreManagementController {

    @Autowired
    private StoreHouseService storeHouseService;

    @PostMapping("/addStore")
    @ResponseBody
    public Result<Boolean> addStore(StoreHouse storeHouse){
        // 判断排序数是否在1到100区间
        if (storeHouse.getSort() > 100 || storeHouse.getSort() < 1) {
            return Result.error(SORT_ERROR);
        }
        storeHouseService.addStore(storeHouse);
        return Result.success(true);
    }
    @PostMapping("/deleteStore")
    @ResponseBody
    // 后期需要加上User权限验证，否则暴露接口有危险。
    public Result<Boolean> deleteStore(int storeId) {
        if (storeHouseService.delectStoreById(storeId)) {
            return Result.success(true);
        }
        return Result.error(STORE_DELETE);
    }

    @GetMapping("/editStoreDiv")
    @ResponseBody
    public StoreHouse editStoreDiv(Integer storeId){
        return storeHouseService.findOneById(storeId);
    }

    @PostMapping("/editStore")
    @ResponseBody
    public Result<Boolean> editStore(StoreHouse storeHouse){
        if(storeHouseService.updateStore(storeHouse)){
            return Result.success(true);
        }
        return Result.error(STORE_UPDATE);
    }
}
